/*//GEN-FIRST:event_formWindowClosing
 * GraphWindow.java//GEN-LAST:event_formWindowClosing
 *
 * Created on den 30 november 2007, 17:08
 */
package grex.Graphics;

import grex.ErrorManagement.ErrorManager;
import grex.ErrorManagement.EvolutionListener;
import grex.EvolutionProcess;
import grex.GP;
import grex.Options;
import grex.Data.Prediction;
import grex.Nodes.Node;
import grex.Nodes.GeneException;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author  RIK
 */
public class GraphWindow extends javax.swing.JPanel implements EvolutionListener, ActionListener {

    /** Creates new form GraphWindow */
    private GP gp;
    private IOutputPanel gPanel;
    private EvolutionProcess ep;
    private boolean isListening = true;
    private TreePanel tp;
    private EvolutionFrame ef;
    
    public GraphWindow(EvolutionProcess ep,EvolutionFrame ef) throws GeneException{
        this(null,ep);
        this.ef=ef;
    }
    
    public GraphWindow(GP gp, EvolutionProcess ep) throws GeneException {
       // initComponents();
        this.setLayout(new BorderLayout());
        this.ep = ep;
        if(gp!=null){
            this.gp = (GP) gp.clone();
            gp.clean();
        }
       //ep.addEvolutionListener(this);
     //   this.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/grex/Graphics/pics/G16.gif")).getImage());
        Options o = ep.getOptions();
        if (o.getPROBLEM_TYPE() == o.CLASSIFICATION) {
            if (gp == null) {
                gPanel = new MatrixPanel(o);
            } else {
                gPanel = new MatrixPanel(gp.getOrderedTrain(),gp.getOrderedTest(),o);
            }
        } else {
            if (gp == null) {
                
                gPanel = new GraphPanel();
            } else {
                gPanel = new GraphPanel(gp.getOrderedTrain(),gp.getOrderedTest());
            }
        }
        add((JPanel)gPanel, BorderLayout.CENTER);
        this.setSize(400, 400);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
    }// </editor-fold>                        
    private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
        ep.removeEvolutionLister(this);
    }                                  
    public void highlightPredictions(JButton prediction){
        
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
            //new GraphWindow().setVisible(true);
            }
        });
    }

    public void evolutionStarted(EvolutionProcess source) {
        
    }

    public void generationTerminated(EvolutionProcess source) {
            if(ef!=null){
                ef.getTreePanel().setActionListener(this);                
            }
            
            if (gp==null || gp.getFitness() != source.getBestGP().getFitness()) {
                gp = (GP) source.getBestGP().clone();
            //    gp.clean();
                gp.train();
                gp.execute(gp.getPcTest());            
                //OBS gPanel is null!
                 if(gPanel!=null)       
                gPanel.init(gp.getOrderedTrain(),gp.getOrderedTest());
            }
    }
    private ArrayList<Node> getLeafs(GP gp){
                ArrayList<Node> leafs = new ArrayList<Node>(); 
                ArrayList<Prediction>  train = gp.getOrderedTrain();
                ArrayList<Prediction>  test = gp.getOrderedTest();
                for(Prediction p: train){
                    leafs.add(p.getGene());
                }
                for(Prediction p: test){
                    leafs.add(p.getGene());
                }
              //  leafs.addAll(gp.getPcTrain().getPredictionLeafs());
              //  leafs.addAll(gp.getPcTest().getPredictionLeafs());
                return leafs;
    }
    public void batchTerminated(EvolutionProcess source) {
        source.removeEvolutionLister(this);
    }

    public void evolutionTerminated(EvolutionProcess source) {
        gp = source.getBestGP();
    }

    public void evolutionProcessTerminated(EvolutionProcess source) {

    }

    public void actionPerformed(ActionEvent e) {        
        GUINode node = (GUINode) e.getSource();
        //GraphPanel g = (GraphPanel) gPanel;

        gPanel.highLightGraphPoints(node.getGene(),getLeafs(node.getGp()));        
    }
    // Variables declaration - do not modify                     
    // End of variables declaration                   
}
